/**
 * @Author lilinjie
 * @Description: 通用筛选获取数据hook
*/

import { useForm } from '@/hooks/common/useForm'
import usePagination from '@/hooks/common/usePagination'
import noop from '@/utils/noop'
import { reactive } from 'vue'

export function useList({ defaultFilterForm = {}, getList = noop }) {
  // 表单
  const { filterForm, clearForm } = useForm({ defaultFilterForm })

  // 分页
  const { pagination, handleCurrentChange, handleSizeChange } = usePagination({ current: 1, pageSize: 15 })

  // 数据
  const tableData = reactive({
    data: []
  })

  // 查询
  function handleSearch() {
    getList({
      filterForm: filterForm, pagination: pagination, onSuccess(data, total) {
        tableData.data = data
        pagination.total = total
        // console.log('data', tableData, 'total', total)
      }
    })
  }

  return { filterForm, clearForm, handleSearch, pagination, handleCurrentChange, handleSizeChange, tableData }
}
